<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 订单统计 开发者: 麦沃德科技-半夏  
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<view class="container-screen flex justify-content-around">
			<view class="screen-item" :class="{active: selectScreen == 1}" @click="changeScreen(1)">日</view>
			<view class="screen-item" :class="{active: selectScreen == 2}" @click="changeScreen(2)">周</view>
			<view class="screen-item" :class="{active: selectScreen == 3}" @click="changeScreen(3)">月</view>
		</view>
		<view class="container-date flex align-items-center">
			<view class="date-left" @click="setLastDate()">
				<image class="icon" src="/static/right.png" mode="aspectFit"></image>
			</view>
			<uni-datetime-picker type="date" @change="changeDate" v-if="selectScreen == 1">
				<view class="date-center flex-item flex flex-center">
					<view class="center-info">{{screenDate.label}}</view>
					<view class="center-icon" :style="{'background-image': 'url('+ downIcon +')'}" v-if="downIcon"></view>
				</view>
			</uni-datetime-picker>
			<view class="date-center flex-item flex flex-center" v-else>
				<view class="center-info">{{screenDate.label}}</view>
			</view>
			<view class="date-right" @click="setNextDate()">
				<image class="icon" src="/static/right.png" mode="aspectFit"></image>
			</view>
		</view>
		<view class="container-main">
			<view class="main-title">累计数据</view>
			<view class="main-column flex align-items-center">
				<view class="column-item">
					<view class="item-value">{{statisticsInfo.total_income}}</view>
					<view class="item-name">收入(元)</view>
				</view>
				<view class="column-line"></view>
				<view class="column-item">
					<view class="item-value">{{statisticsInfo.acceptance_number}}</view>
					<view class="item-name">接单数量</view>
				</view>
			</view>
			<view class="main-title">提现数据</view>
			<view class="main-column flex align-items-center">
				<view class="column-item">
					<view class="item-value">{{statisticsInfo.pending_withdrawal}}</view>
					<view class="item-name">待提现(元)</view>
				</view>
				<view class="column-line"></view>
				<view class="column-item">
					<view class="item-value">{{statisticsInfo.already_withdrawn}}</view>
					<view class="item-name">已提现(元)</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import svgData from "@/common/svg.js"
	export default {
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				downIcon: state => {
					return svgData.svgToUrl("down", state.app.themeColor)
				},
			})
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 已选筛选
				selectScreen: 1,
				// 筛选日期
				screenDate: {},
				// 统计数据
				statisticsInfo: {},
			}
		},
		onLoad() {
			this.screenDate = {
				label: this.$util.getCurrentDate("m月d日 W"),
				startDate: this.$util.getCurrentDate("y-m-d"),
				endDate: this.$util.getCurrentDate("y-m-d"),
			}
			uni.showLoading({
				title: "加载中"
			})
			this.getStatistics(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		methods: {
			// 更换已选筛选
			changeScreen(id) {
				this.selectScreen = id
				if (id == 1) {
					this.screenDate = {
						label: this.$util.getCurrentDate("m月d日 W"),
						startDate: this.$util.getCurrentDate("y-m-d"),
						endDate: this.$util.getCurrentDate("y-m-d"),
					}
				} else if (id == 2) {
					let nowDate = this.$util.getCurrentDate("y-m-d")
					this.screenDate = {
						label: `${this.$util.beginOfWeek(nowDate, "m月d日")}-${this.$util.endOfWeek(nowDate, "m月d日")}`,
						startDate: this.$util.beginOfWeek(nowDate, "y-m-d"),
						endDate: this.$util.endOfWeek(nowDate, "y-m-d"),
					}
				} else if (id == 3) {
					let nowDate = this.$util.getCurrentDate("y-m-d")
					this.screenDate = {
						label: this.$util.getCurrentDate("m月"),
						startDate: this.$util.beginOfMonth(nowDate, "y-m-d"),
						endDate: this.$util.endOfMonth(nowDate, "y-m-d"),
					}
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.getStatistics(() => uni.hideLoading())
			},
			// 获取统计数据
			getStatistics(fn) {
				this.$util.request("driver.statistics", {
					start_date: this.screenDate.startDate,
					end_date: this.screenDate.endDate,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.statisticsInfo = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取统计数据 ', error)
				})
			},
			// 更改日期
			changeDate(date) {
				this.screenDate = {
					label: this.$util.getFormatDate(date, "m月d日 W"),
					startDate: date,
					endDate: date,
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.getStatistics(() => uni.hideLoading())
			},
			// 设置上一日/周/月
			setLastDate() {
				if (this.selectScreen == 1) {
					this.screenDate = {
						label: this.$util.getBeforeDate(this.screenDate.startDate, 3, 1, "m月d日 W"),
						startDate: this.$util.getBeforeDate(this.screenDate.startDate, 3, 1, "y-m-d"),
						endDate: this.$util.getBeforeDate(this.screenDate.startDate, 3, 1, "y-m-d"),
					}
				} else if (this.selectScreen == 2) {
					this.screenDate = {
						label: `${this.$util.getBeforeDate(this.screenDate.startDate, 3, 7, "m月d日")}-${this.$util.getBeforeDate(this.screenDate.endDate, 3, 7, "m月d日")}`,
						startDate: this.$util.getBeforeDate(this.screenDate.startDate, 3, 7, "y-m-d"),
						endDate: this.$util.getBeforeDate(this.screenDate.endDate, 3, 7, "y-m-d"),
					}
				} else if (this.selectScreen == 3) {
					let startDate = this.$util.getBeforeDate(this.screenDate.startDate, 2, 1, "y-m-d")
					this.screenDate = {
						label: this.$util.getBeforeDate(this.screenDate.startDate, 2, 1, "m月"),
						startDate: startDate,
						endDate: this.$util.endOfMonth(startDate, "y-m-d"),
					}
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.getStatistics(() => uni.hideLoading())
			},
			// 设置下一日/周/月
			setNextDate() {
				if (this.selectScreen == 1) {
					this.screenDate = {
						label: this.$util.getAfterDate(this.screenDate.startDate, 3, 1, "m月d日 W"),
						startDate: this.$util.getAfterDate(this.screenDate.startDate, 3, 1, "y-m-d"),
						endDate: this.$util.getAfterDate(this.screenDate.startDate, 3, 1, "y-m-d"),
					}
				} else if (this.selectScreen == 2) {
					this.screenDate = {
						label: `${this.$util.getAfterDate(this.screenDate.startDate, 3, 7, "m月d日")}-${this.$util.getAfterDate(this.screenDate.endDate, 3, 7, "m月d日")}`,
						startDate: this.$util.getAfterDate(this.screenDate.startDate, 3, 7, "y-m-d"),
						endDate: this.$util.getAfterDate(this.screenDate.endDate, 3, 7, "y-m-d"),
					}
				} else if (this.selectScreen == 3) {
					let startDate = this.$util.getAfterDate(this.screenDate.startDate, 2, 1, "y-m-d")
					this.screenDate = {
						label: this.$util.getAfterDate(this.screenDate.startDate, 2, 1, "m月"),
						startDate: startDate,
						endDate: this.$util.endOfMonth(startDate, "y-m-d"),
					}
				}
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				this.getStatistics(() => uni.hideLoading())
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx;

		.container-screen {
			padding: 16rpx;
			border-radius: 32rpx;
			background: #FFF;

			.screen-item {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				background: transparent;
				color: #242629;
				font-size: 32rpx;
				font-weight: 600;
				line-height: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				&.active {
					background: var(--theme-color);
					color: #FFF;
				}
			}
		}

		.container-date {
			margin-top: 32rpx;
			padding: 30rpx 22rpx;
			border-radius: 32rpx;
			background: #FFF;

			.date-left {
				width: 44rpx;
				height: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				transform: rotate(180deg);

				.icon {
					width: 24rpx;
					height: 24rpx;
				}
			}

			.date-center {
				.center-info {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;
					text-align: center;
				}

				.center-icon {
					margin-left: 8rpx;
					width: 24rpx;
					height: 24rpx;
					background-size: 24rpx;
					background-repeat: no-repeat;
				}
			}

			.date-right {
				width: 44rpx;
				height: 44rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				.icon {
					width: 24rpx;
					height: 24rpx;
				}
			}
		}

		.container-main {
			margin-top: 32rpx;
			padding: 32rpx;
			border-radius: 32rpx;
			background: #FFF;

			.main-title {
				color: #242629;
				font-size: 32rpx;
				font-weight: 600;
				line-height: 44rpx;
				margin-top: 32rpx;

				&:first-child {
					margin-top: 0;
				}
			}

			.main-column {
				margin-top: 24rpx;

				.column-item {
					width: 50%;

					.item-value {
						color: var(--theme-color);
						text-align: center;
						font-size: 40rpx;
						font-weight: 600;
						line-height: 56rpx;
					}

					.item-name {
						margin-top: 8rpx;
						color: #8D929C;
						text-align: center;
						font-size: 24rpx;
						line-height: 34rpx;
					}
				}

				.column-line {
					width: 2rpx;
					height: 80rpx;
					background: #EBEBEB;
				}
			}
		}
	}
</style>